Typical HAL SMBUS use cases for migration

This section provides information about typical HAL SMBUS use cases for migration.

Note

The processes for transmitting and receiving data using interrupt are the same in HAL1 and HAL2, except for the specification of buffer size: HAL1 uses Size for the number of data elements (u16), while HAL2 uses Size_byte for the buffer size in bytes.

Use case: SMBUS master transmit in interrupt mode

Sequence diagram

SMBUS master transmit in interrupt mode

  @startuml
    title : <font size:28>SMBUS mastert transmit interruption mode

    participant "User Application\n(thread mode)" as appli
    participant "User Application\n(handler mode)" as appli_IRQ
    participant "<font color=green><b>HAL SMBUS</b></font>" as SMBUS
    participant "System HW interruption" as interruption

    group#Linen SMBUS operation

      appli->SMBUS : <font color=green><b>HAL_SMBUS_Master_Transmit_IT()
      return
      SMBUS->SMBUS : Enable SMBUS selected interruptions
      interruption <[#red]-x? : <font color=red><b>SMBUS IRQ event
      interruption <[#red]-x? : <font color=red><b>SMBUS IRQ error
      interruption -> appli_IRQ : I2Cx_EV_IRQHandler()
      activate appli_IRQ #DarkSalmon
      appli_IRQ -> SMBUS : HAL_SMBUS_EV_IRQHandler()
      interruption -> appli_IRQ : I2Cx_ER_IRQHandler()
      appli_IRQ -> SMBUS : HAL_SMBUS_ER_IRQHandler()
      return
      deactivate appli_IRQ

      SMBUS -> appli_IRQ: <font color=green><b>HAL_SMBUS_MasterTxCpltCallback()
      SMBUS -> appli_IRQ: <font color=green><b>HAL_SMBUS_ErrorCallback()
      return
      SMBUS -> appli_IRQ: <font color=green><b>HAL_SMBUS_GetError()
      return

    @enduml

Code snippet

HAL1

HAL2

#define SMBUS_ADDRESS          0x42

/* Buffer used for SMBUS transmission */
uint8_t aTxBuffer[] = " ****SMBUS communication based on IT**** ";

/* Size of SMBUS Transmission buffer */
#define TXBUFFERSIZE  (COUNTOF(aTxBuffer) - 1)

void main()
{

  // Add your Init and config code

  /* Start the transmission process */
  HAL_SMBUS_Master_Transmit_IT(&hsmbus2, (uint16_t)SMBUS_ADDRESS, (uint8_t*)aTxBuffer, TXBUFFERSIZE, SMBUS_FIRST_AND_LAST_FRAME_NO_PEC);

  /*wait end of SMBUS tranfert*/

  while (1)
  {
  }
}

/* SMBUS transfer complete callback */
void HAL_SMBUS_MasterTxCpltCallback(SMBUS_HandleTypeDef *hsmbus)
{
  /* Transmission complete */
  // Add your post-transfer code here
}

/* SMBUS error callback */
void HAL_SMBUS_ErrorCallback(SMBUS_HandleTypeDef *SMBUSHandle)
{
  if (HAL_SMBUS_GetError(SMBUSHandle) != HAL_SMBUS_ERROR_ACKF)
  {
    /* Handle the error here */
  }
}

/* SMBUS event interrupt handler */
void I2C2_EV_IRQHandler(void)
{
  HAL_SMBUS_EV_IRQHandler(&hsmbus2);
}

/* SMBUS error interrupt handler */
void I2C2_ER_IRQHandler(void)
{
  HAL_SMBUS_ER_IRQHandler(&hsmbus2);
}
#define SMBUS_ADDRESS          0x42

/* Buffer used for SMBUS transmission */
uint8_t aTxBuffer[] = " ****SMBUS communication based on IT**** ";

/* Size of SMBUS Transmission buffer */
#define TXBUFFERSIZE  (COUNTOF(aTxBuffer) - 1)

void main()
{
  // Add your Init and config code

  /* Start the transmission process */
  HAL_SMBUS_MASTER_SEQ_Transmit_IT(&hsmbus2, (uint16_t)SMBUS_ADDRESS, (uint8_t*)aTxBuffer, TXBUFFERSIZE, HAL_SMBUS_XFER_FIRST_AND_LAST_FRAME_NO_PEC);

  /*wait end of SMBUS tranfert*/
  while (hsmbus2.global_state != HAL_SMBUS_STATE_IDLE);

  while (1)
  {
  }
}

/* SMBUS error callback */
void HAL_SMBUS_ErrorCallback(hal_smbus_handle_t *hsmbus2)
{
  if (HAL_SMBUS_GetLastErrorCodes(hsmbus2) != HAL_SMBUS_ERROR_ACKF)
  {
    /* Handle the error here */
  }
}

/* SMBUS event interrupt handler */
void I2C2_EV_IRQHandler(void)
{
  HAL_SMBUS_EV_IRQHandler(&hsmbus2);
}

/* SMBUS error interrupt handler */
void I2C2_ER_IRQHandler(void)
{
  HAL_SMBUS_ER_IRQHandler(&hsmbus2);
}

Key changes

  1. The transmit API changes from HAL_SMBUS_Master_Transmit_IT() to HAL_SMBUS_MASTER_SEQ_Transmit_IT().

  2. Transfer options use the HAL_SMBUS_XFER_* naming scheme in HAL2.

  3. The buffer size parameter is expressed in bytes in HAL2 (Size_byte) instead of the number of data elements in HAL1 (Size).

  4. Error retrieval uses HAL_SMBUS_GetLastErrorCodes() in HAL2.

Use case: SMBUS slave receive in interrupt mode

Sequence diagram

SMBUS slave receive in interrupt mode

  @startuml
    title : <font size:28>SMBUS slave receive interruption mode

    participant "User Application\n(thread mode)" as appli
    participant "User Application\n(handler mode)" as appli_IRQ
    participant "<font color=green><b>HAL SMBUS</b></font>" as SMBUS
    participant "System HW interruption" as interruption

    group#Linen SMBUS operation

      appli->SMBUS : <font color=green><b>HAL_SMBUS_Slave_Receive_IT()
      return

      SMBUS->SMBUS : Enable SMBUS selected interruptions
      interruption <[#red]-x? : <font color=red><b>SMBUS IRQ event
      interruption <[#red]-x? : <font color=red><b>SMBUS IRQ error
      interruption -> appli_IRQ : I2Cx_EV_IRQHandler()
      activate appli_IRQ #DarkSalmon
      appli_IRQ -> SMBUS : HAL_SMBUS_EV_IRQHandler()
      interruption -> appli_IRQ : I2Cx_ER_IRQHandler()
      appli_IRQ -> SMBUS : HAL_SMBUS_ER_IRQHandler()
      return
      deactivate appli_IRQ

      SMBUS -> appli_IRQ: <font color=green><b>HAL_SMBUS_AddrCallback()
      return

      SMBUS -> appli_IRQ: <font color=green><b>HAL_SMBUS_ErrorCallback()
      return
      SMBUS -> appli_IRQ: <font color=green><b>HAL_SMBUS_GetError()
      return

    @enduml

Code snippet

HAL1

HAL2

#define SMBUS_ADDRESS          0x42

/* Buffer used for SMBUS transmission */
uint8_t aTxBuffer[] = " ****SMBUS_TwoBoards communication using autonomous mode functionality**** ";

 /* Buffer used for SMBUS reception */
 uint8_t aRxBuffer[RXBUFFERSIZE];

/* Size of SMBUS Transmission buffer */
#define RXBUFFERSIZE  (COUNTOF(aTxBuffer) - 1)

void main()
{
  // Add your Init and config code

  /* Enable address Match detect with interrupt */
  HAL_SMBUS_EnableListen_IT(&hsmbus2);

  /* Wait until address matched */

  /* Put SMBUS peripheral in reception process  */
  HAL_SMBUS_Slave_Receive_IT(&hsmbus2, (uint8_t *)aRxBuffer, RXBUFFERSIZE, SMBUS_FIRST_AND_LAST_FRAME_NO_PEC);

  /*wait end of SMBUS tranfert*/

  /* Compare the sent and received buffers */

  while (1)
  {
  }
}

/* Slave address match callbacks */
void HAL_SMBUS_AddrCallback(SMBUS_HandleTypeDef *hsmbus,uint8_t TransferDirection, uint16_t AddrMatchCode)
 {
   /* Get the slave address match code */
 }

/* SMBUS error callback */
 void HAL_SMBUS_ErrorCallback(SMBUS_HandleTypeDef *SMBUSHandle)
 {

   if (HAL_SMBUS_GetError(SMBUSHandle) != HAL_SMBUS_ERROR_ACKF)
   {
    /* Handle the error here */
   }
 }

/* SMBUS event interrupt handler */
void I2C2_EV_IRQHandler(void)
{
  HAL_SMBUS_EV_IRQHandler(&hsmbus2);
}

/* SMBUS error interrupt handler */
void I2C2_ER_IRQHandler(void)
{
  HAL_SMBUS_ER_IRQHandler(&hsmbus2);
}
#define SMBUS_ADDRESS          0x42

/* Buffer used for SMBUS transmission */
uint8_t aTxBuffer[] = " ****SMBUS_TwoBoards communication using autonomous mode functionality**** ";

 /* Buffer used for SMBUS reception */
 uint8_t aRxBuffer[RXBUFFERSIZE];

/* Size of SMBUS Transmission buffer */
#define RXBUFFERSIZE  (COUNTOF(aTxBuffer) - 1)


void main()
{

  hal_smbus_handle_t hsmbus2;

  // Add your Init and config code

  /* Enable address Match detect with interrupt */
  HAL_SMBUS_SLAVE_EnableListen_IT(&hsmbus2);

  /* Put SMBUS peripheral in reception process  */
  HAL_SMBUS_SLAVE_SEQ_Receive_IT(&hsmbus2, (uint8_t *)aRxBuffer, RXBUFFERSIZE, HAL_SMBUS_XFER_FIRST_AND_LAST_FRAME_NO_PEC);

  /*wait end of SMBUS tranfert*/
  while (hsmbus2.global_state != HAL_SMBUS_STATE_IDLE);

  /* Compare the sent and received buffers */

  while (1)
  {
  }
}

/* SMBUS event interrupt handler */
void I2C2_EV_IRQHandler(void)
{
  HAL_SMBUS_EV_IRQHandler(&hsmbus2);
}

/* SMBUS error interrupt handler */
void I2C2_ER_IRQHandler(void)
{
  HAL_SMBUS_ER_IRQHandler(&hsmbus2);
}

/* Slave address match callbacks */
void HAL_SMBUS_AddrCallback(SMBUS_HandleTypeDef *hsmbus,uint8_t TransferDirection, uint16_t AddrMatchCode)
 {
   /* Get the slave address match code */
 }

/* SMBUS error callback */
 void HAL_SMBUS_ErrorCallback(SMBUS_HandleTypeDef *SMBUSHandle)
 {

   if (HAL_SMBUS_GetError(SMBUSHandle) != HAL_SMBUS_ERROR_ACKF)
   {
    /* Handle the error here */
   }
 }

Key changes

  1. Listen enable API changes from HAL_SMBUS_EnableListen_IT() to HAL_SMBUS_SLAVE_EnableListen_IT().

  2. Slave receive API changes from HAL_SMBUS_Slave_Receive_IT() to HAL_SMBUS_SLAVE_SEQ_Receive_IT().

  3. Transfer options use the HAL_SMBUS_XFER_* naming scheme in HAL2.

  4. The buffer size parameter is expressed in bytes in HAL2 (Size_byte) instead of the number of data elements in HAL1 (Size).

Related examples in HAL2

Example name

Brief

hal_smbus_two_boards_com_it_autonomous_slave

How to handle data buffer transmission between two boards via SMBUS using IT mode.